home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 288_01.zip / BLDMTX.C < prev    next >
Text File  |  1993-04-01  |  7KB  |  189 lines

  1. /*
  2.         HEADER:         CUG000.12;
  3.         TITLE:          BuildMatrix (main), InputMatrix, OutputMatrix;
  4.         DATE:           Mar 89;
  5.         DESCRIPTION:    Prompts User for Data to Build Distance Matrix;
  6.         VERSION:        2.0;
  7.         FILENAME:       BLDMTX.C;
  8.         SEE-ALSO:       TSP.C, NN.C, POPT.C, 2OPT.C, HYBRID.C, 3OPT.C, FN.C,
  9.                         BOOLEAN.H, NODELIST.H, TSP.H,
  10.                         PRTMTX.C, TIME.C;
  11.         AUTHORS:        Kevin E. Knauss;
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <tsp.h>
  16.  
  17. main ()
  18. {
  19.    char *Names[];
  20.    unsigned Network[MAXROWS][MAXCOLS];
  21.    unsigned NumberOfVirteces;
  22.    char *filename;
  23.    FILE *fp;
  24.  
  25.    printf("\n\n");
  26.    NumberOfVirteces = InputMatrix (Network, Names);
  27.    /* Open output and print headers */
  28.       printf("\n\n\n****************************************");
  29.       printf("\n\nInput filename for output: ");
  30.       if (gets(filename) == NULL) {
  31.          printf("\n*******************************************");
  32.          printf("\n   Execution Terminated At User Request!");
  33.          printf("\n*******************************************\n\n");
  34.          exit(-1);
  35.       }
  36.       printf("\n\nOutput will be written to: %s\n\n", filename);
  37.       if (filename == "stdout")
  38.          fp = stdout;
  39.       else
  40.          if ((fp = fopen (filename, "w")) == NULL) {
  41.             printf("\n*******************************************");
  42.             printf("\n   Execution Terminated - Invalid Open!");
  43.             printf("\n*******************************************\n\n");
  44.             exit(-1);
  45.          }
  46.    OutputMatrix (fp, Network, Names, NumberOfVirteces);
  47.    /* Clean up and go */
  48.       fclose (fp);
  49.       printf("\n*******************************************");
  50.       printf("\n        Execution Terminated Normally!");
  51.       printf("\n*******************************************\n\n");
  52. } /* BuildMatrix - main */
  53.    
  54.  
  55. unsigned InputMatrix (Network, Names)
  56.    unsigned *Network[];
  57.    char *Names[];
  58. {
  59.    unsigned NumberOfVirteces;
  60.    unsigned index, FirstIndex, SecondIndex, dist;
  61.    char *Answer;
  62.    char ch;
  63.    long atol ();
  64.    printf("Input number of virteces to be entered: ");
  65.    scanf ("%u", NumberOfVirteces);
  66.    for ( index = 1; index <=  NumberOfVirteces; index++) {
  67.       printf("Input name for virtex #%2u: ", index);
  68.       scanf("%s", Names [index]);
  69.    }
  70.    printf("Are the distances between virteces symetric? (Y|N): ");
  71.    scanf ("%s", Answer);
  72.    if ((Answer [0] == 'n') || (Answer [0] == 'N')) {
  73.       FirstIndex = 1;
  74.       do {
  75.          SecondIndex = 1;
  76.          do {
  77.             if (FirstIndex != SecondIndex) {
  78.                printf("Input distance from %s", Names [FirstIndex]);
  79.                printf(" to %s:", Names [SecondIndex]);
  80.                scanf ("%s", Answer);
  81.                ch = Answer [0];
  82.                if ((ch == 'b') || (ch == 'B') ||
  83.                   ((ch != NULL) && (ch < ' '))) {
  84.                   FirstIndex = NumberOfVirteces;
  85.                   SecondIndex = FirstIndex + 1;
  86.                }
  87.                if (ch == NULL) {
  88.                   printf("\nWARNING: Matrix will not be complete!\n");
  89.                   Network [FirstIndex][SecondIndex] = MAXCARD;
  90.                   SecondIndex++;
  91.                } else {
  92.                   if ((ch != 'd') && (ch != 'D'))
  93.                      dist = (unsigned) atol(Answer);
  94.                   if (dist > 0) {
  95.                      Network [FirstIndex][SecondIndex] = dist;
  96.                      SecondIndex = SecondIndex + 1;
  97.                   } else {
  98.                      if (ch == '-') {
  99.                         do {
  100.                            if (--SecondIndex < 1) {
  101.                               if (FirstIndex > 1) {
  102.                                  FirstIndex--;
  103.                                  SecondIndex = NumberOfVirteces;
  104.                               } else {
  105.                                  FirstIndex = 1;
  106.                                  SecondIndex = 2;
  107.                               }
  108.                            }
  109.                         } while (FirstIndex == SecondIndex);
  110.                      }
  111.                   }
  112.                }
  113.             } else {
  114.                Network [FirstIndex][SecondIndex] = MAXCARD;
  115.                SecondIndex++;
  116.             }
  117.          } while (SecondIndex <= NumberOfVirteces);
  118.          FirstIndex++;
  119.       } while (FirstIndex <= NumberOfVirteces);
  120.    } else {
  121.       FirstIndex = 1;
  122.       do {
  123.          SecondIndex = FirstIndex;
  124.          do {
  125.             if (FirstIndex != SecondIndex) {
  126.                printf("Input distance from %s", Names [FirstIndex]);
  127.                printf(" to %s: ", Names [SecondIndex]);
  128.                gets (Answer);
  129.                ch = Answer [0];
  130.                if ((ch == 'b') || (ch == 'B') ||
  131.                   ((ch != NULL) && (ch < ' '))) {
  132.                   FirstIndex = NumberOfVirteces;
  133.                   SecondIndex = FirstIndex + 1;
  134.                }
  135.                if (ch == NULL) {
  136.                   printf("\nWARNING: Matrix will not be complete!");
  137.                   Network [FirstIndex][SecondIndex] = MAXCARD;
  138.                   Network [SecondIndex][FirstIndex] = MAXCARD;
  139.                   SecondIndex++;
  140.                } else {
  141.                   if ((ch != 'd') && (ch != 'D'))
  142.                      dist = (unsigned) atol (Answer);
  143.                   if (dist > 0) {
  144.                      Network [FirstIndex][SecondIndex] = dist;
  145.                      Network [SecondIndex][FirstIndex] = dist;
  146.                      SecondIndex++;
  147.                   } else { 
  148.                      if (ch == '-') {
  149.                         if (--SecondIndex <= FirstIndex) {
  150.                            if (FirstIndex > 1) {
  151.                               FirstIndex--;
  152.                               SecondIndex = NumberOfVirteces;
  153.                            } else {
  154.                               FirstIndex = 1;
  155.                               SecondIndex = 2;
  156.                            }
  157.                         }
  158.                      }
  159.                   }
  160.                }
  161.             } else {
  162.                Network [FirstIndex][SecondIndex] = MAXCARD;
  163.                SecondIndex++;
  164.             }
  165.          } while (SecondIndex <= NumberOfVirteces);
  166.          FirstIndex++;
  167.       } while (FirstIndex <= NumberOfVirteces);
  168.    }
  169.    return (NumberOfVirteces);
  170. } /* InputMatrix */
  171.  
  172.  
  173. unsigned OutputMatrix (fp, Network, Names, NumberOfVirteces)
  174.    FILE *fp;
  175.    unsigned *Network[];
  176.    char *Names[];
  177.    unsigned NumberOfVirteces;
  178. {   
  179.    unsigned index, FirstIndex, SecondIndex;
  180.    char *Answer;
  181.  
  182.    printf("%2u\n", NumberOfVirteces);
  183.    for ( index = 1; index <=  NumberOfVirteces; index++)
  184.       printf("%s\n", Names [index]);
  185.    for ( FirstIndex = 1; FirstIndex <=  NumberOfVirteces; FirstIndex++)
  186.       for ( SecondIndex = 1; SecondIndex <=  NumberOfVirteces; SecondIndex++)
  187.          printf("%5u\n", Network [FirstIndex][SecondIndex]);
  188. } /* OutputMatrix */
  189.